package com.yyx.jupiter.service.recommend.data;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.yyx.jupiter.component.RedisClient;
import com.yyx.jupiter.entity.UpdateVideo;
import com.yyx.jupiter.utils.JsonUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ItemProfileService {

    @Resource
    RedisClient redisClient;

    public void updateVideoProfile(UpdateVideo updateVideo) {
        String redisKey = "yyx:video:" + updateVideo.getVideoId();
        Map<String, Object> videoProfileMap = JSONObject.parseObject(
                JsonUtils.toJsonString(updateVideo),
                new TypeReference<HashMap<String, Object>>() {}
        );
        redisClient.hSetAll(redisKey, videoProfileMap);
        redisClient.setTTL(redisKey, 60L * 60 * 24 * 2);
    }

    public void addVideoRemoveList(String videoId) {
        String redisKey = "yyx:video_remove";
        String removeVideos = redisClient.get(redisKey, String.class);
        HashSet<String> removeVideoSet = new HashSet<>();
        if (removeVideos != null) {
            removeVideoSet.addAll(Arrays.stream(removeVideos.split(",")).toList());
        }
        removeVideoSet.add(videoId);
        removeVideos = String.join(",", removeVideoSet);
        redisClient.setEx(redisKey, removeVideos, 60L * 60 * 24 * 2);
    }

    public void updateVideoRemoveList(String videoId) {
        String redisKey = "yyx:video_remove";
        String redisObj = redisClient.get(redisKey, String.class);
        if (redisObj == null) {
            return;
        }
        List<String> removeVideos = Arrays.asList(redisObj.split(","));
        removeVideos.remove(videoId);
        redisClient.setEx(redisKey, String.join(",", removeVideos), 60L * 60 * 24 * 2);
    }
}
